﻿using System;
using System.Text;
using System.Linq;
using Microsoft.VisualStudio.ExtensibilityHosting;
using System.Globalization;
using System.ComponentModel.Composition;
using ExtensionsMadeEasy.Utilities.ValueConverters;

namespace ExtensionsMadeEasy.Utilities.Extensions
{
    public static class StringExtensions
    {
        /// <summary>
        /// Returns a fuzzy version of this string.
        /// </summary>
        /// <param name="original">The original version</param>
        /// <returns>A fuzzy version of the string</returns>
        public static string MakeFuzzy(this string original)
        {
            if (string.IsNullOrEmpty(original))
                return string.Empty;
            else
                return original
                .ToUpper()
                .ReplaceDiacriticWithAlphabetic()
                .RemoveNonAlphabetic()
                .MakeMethaphonic()
                .Replace(" ", "");
        }

        internal static string MakeMethaphonic(this string original)
        {
            var converter = VsExportProviderService
                .GetExportedValues<IMethaphonicStringValueConverter>()
                .Where(x => x.CanConvert(original))
                .FirstOrDefault();
            if (converter == null)
                converter = new DutchMethaphoneStringConverter();
            return converter.Convert(original);

        }

        internal static string RemoveNonAlphabetic(this string searchString)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < searchString.Length; i++)
            {
                char c = searchString[i];
                if (' '.Equals(c) || (int)c >= 65 && (int)c <= 90)
                {
                    sb.Append(c);
                }
            }
            return sb.ToString();
        }


        internal static string ReplaceDiacriticWithAlphabetic(this string searchString)
        {
            //http://pastebin.com/CJdrG2nt             
            searchString.Replace("À", "A");
            searchString.Replace("Á", "A");
            searchString.Replace("Â", "A");
            searchString.Replace("Ã", "A");
            searchString.Replace("Ä", "A");
            searchString.Replace("Å", "A");
            searchString.Replace("Æ", "A");

            searchString.Replace("Ç", "C");

            searchString.Replace("È", "E");
            searchString.Replace("É", "E");
            searchString.Replace("Ê", "E");
            searchString.Replace("Ë", "E");

            searchString.Replace("Ì", "I");
            searchString.Replace("Í", "I");
            searchString.Replace("Î", "I");
            searchString.Replace("Ï", "I");

            searchString.Replace("Ñ", "N");

            searchString.Replace("Ò", "O");
            searchString.Replace("Ó", "O");
            searchString.Replace("Ô", "O");
            searchString.Replace("Õ", "O");
            searchString.Replace("Ö", "O");

            searchString.Replace("Ù", "U");
            searchString.Replace("Ú", "U");
            searchString.Replace("Û", "U");
            searchString.Replace("Ü", "U");

            searchString.Replace("Ý", "Y");

            searchString.Replace("Æ", "AE");
            searchString.Replace("Œ", "OE");
            return searchString;
        }
    }

}
